﻿/*
	***Copyright 2012, Gabriel Valentoni Guelfi***
	
	
	This file is part of GWebDev Actionscript API.
	
	GWebDev Actionscript API is a free software; you can redistribute it
 	and/or modify it within the terms of General Public License GNU the way it was released by Free Software Foundation.
	
	This program is distributed with the hope that can be useful, but UNWARRANTED; without implicit warranties of adequation
	to any market or application in particular. See the General Public License GNU for more details.
	
	You shall has received a copy of the General Public License GNU along with this software within a file named "license.txt", 
	if you hasn´t, please, subscribe to Free Software Foundation Inc., 51 Franklin St., Fifth Floor, Boston, MA 02110-1301, USA.
	Or contact the author trough the e-mail address listed below.

*/

////// GWebDev Actionscript API //////
//
// Version: 0.1
// Author: Gabriel Valentoni Guelfi
// E-mail: gabriel.valguelfi@gmail.com
//
//////

package gwdflash.display{
	import flash.net.URLRequest;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	public class GWDPreloader extends MovieClip{
		include '../net/net_lib.as';
		
		private var attachments:Array;
		
		private var issetProgressBar:Boolean = false;
		private var bar:MovieClip;
		
		private var issetPercentageShower:Boolean = false;
		private var percTxtField:TextField;

		public function GWDPreloader() {
			attachments = new Array();
		}
		
		public function setLoadMovie(movie:MovieClip):void {
			addChildAt(movie, 0);
			attachments.push(movie);
		}
		
		public function setProgressBar(bar_x:Number, bar_y:Number, BGcolor:Number, barColor:Number, barWidth:Number, barHeight:Number):void {
			issetProgressBar = true;
			var barContainer:MovieClip = new MovieClip();
			
			var barBG:MovieClip = new MovieClip();
			barBG.graphics.beginFill(BGcolor);
			barBG.graphics.drawRect(0, 0, barWidth, barHeight);
			barBG.graphics.endFill();
			
			bar = new MovieClip();
			bar.graphics.beginFill(barColor);
			bar.graphics.drawRect(0, 0, barWidth, barHeight);
			bar.graphics.endFill();
			bar.scaleX = 0.0;
			
			barContainer.addChild(barBG);
			barContainer.addChild(bar);
			
			attachments.push(barContainer);
			barContainer.x = bar_x;
			barContainer.y = bar_y;
			addChild(barContainer);
		}
		
		public function setPercentageShower(perc_x:Number, perc_y:Number, color:Number, size:Number, fontFace:String, fontStyle:String):void {
			issetPercentageShower = true;
			var showerContainer:MovieClip = new MovieClip();
			
			var percTxtFormat:TextFormat = new TextFormat();
			percTxtFormat.align = 'center';
			percTxtFormat.color = color;
			percTxtFormat.size = size;
			percTxtFormat.font = fontFace;
			switch(fontStyle) {
				case 'italic':
					percTxtFormat.italic = true;
					break;
				case 'bold':
					percTxtFormat.bold = true;
					break;
				default:
					percTxtFormat.italic = false;
					percTxtFormat.bold = false;
					break;
			}
			
			percTxtField = new TextField();
			percTxtField.text = "0%";
			percTxtField.defaultTextFormat = percTxtFormat;
			percTxtField.selectable = false;
			
			showerContainer.addChild(percTxtField);
			
			attachments.push(showerContainer);
			showerContainer.x = perc_x;
			showerContainer.y = perc_y;
			addChild(showerContainer);
		}
		
		public function startLoad(_fileURL:String, _vars:Object = null):void {
			var fileURL:String = _fileURL;
			if(_vars != null){
				fileURL = URIQueryString(fileURL, _vars);
			}

			var genericLoader:Loader = new Loader();
			genericLoader.load(new URLRequest(fileURL));
			genericLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loading);
			genericLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showContent);

			function loading(e:ProgressEvent):void {
				var perc:int = (e.bytesLoaded / e.bytesTotal) * 100;
				if(issetProgressBar) {
					bar.scaleX = e.bytesLoaded / e.bytesTotal;
				}
				if(issetPercentageShower) {
					percTxtField.text = String(perc) + "%";
				}
			}
			
			function showContent(e:Event):void {
				genericLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loading);
				genericLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, showContent);
				
				for(var j:int = 0; j < attachments.length; j++) {
					removeChild(attachments[j]);
				}
				addChild(genericLoader);
			}
		}
	}
}